cap log close 
log using "${logpath}emergency_analysis.log", replace

/*******************************************************************************
emergency_analysis.do

This code incorporates emergency ride data in district- and firm-level data
*******************************************************************************/

qui {
	use "${cleandatapath}ridelevel_emerg.dta" if year>=2012, replace

	gen emerg_pmt=emergency*tot_pmt
	gen NEdial_pmt=NE_hometodial*tot_pmt
	gen Edial=emergency if (orig_cat2=="Home" | orig_cat2=="ESRD") & (dest_cat2=="Home" | dest_cat2=="ESRD")
	gen nonEdial=Edial==0 if !missing(Edial)
	gen nonEdial_pmt=nonEdial*tot_pmt

	gen tot_rides=1
	collapse (sum) tot_rides NEdial_rides=NE_hometodial NEdial_pmt nonEdial_pmt, by(NPI state t date month year)

	drop if missing(state) | missing(NPI) | missing(date)
	egen firmstate=group(NPI state)

	tsset firmstate t
	tsfill

	bysort t: egen datefill=max(date)
	replace date=datefill
	foreach var in state NPI {
		bysort firmstate: egen fillin=mode(`var')
		replace `var'=fillin if missing(`var')
		drop fillin
	}

	save "${cleandatapath}firmlevel.dta", replace

	gen prior_auth=0 if !missing(state) & !missing(date)
	replace prior_auth=1 if (state==34 | state==42 | state==45) & date>=20058
	replace prior_auth=1 if (state==10 | state==11 | state==24 | state==37 | state==51 | state==54) & date>=20454
	gen priorauth_date=0 if !missing(state)
	replace priorauth_date=1 if (state==34 | state==42 | state==45)
	replace priorauth_date=2 if (state==10 | state==11 | state==24 | state==37 | state==51 | state==54)

	foreach var in tot_rides NEdial_rides {
		replace `var'=0 if missing(`var')
		gen log_`var'=log(`var'+1)
	}

	gen postPA = 0 if (priorauth_date==1 & date<20058 & t>=mofd(20058)-36) | (priorauth_date==2 & date<20454 & t>=mofd(20454)-36)
	replace postPA = 1 if (priorauth_date==1 & date>=20058 & t<mofd(20058)+36) | (priorauth_date==2 & date>=20454 & t<mofd(20454)+36)
	
	bysort firmstate postPA: egen per_rides=total(tot_rides)
	bysort firmstate postPA: egen per_NErides=total(NEdial_rides)
	gen per_pre_rides=per_rides if postPA==0
	gen per_post_rides=per_rides if postPA==1
	gen per_pre_NErides=per_NErides if postPA==0
	gen per_post_NErides=per_NErides if postPA==1
	bysort firmstate: egen per_pre_firm_rides=max(per_pre_rides)
	bysort firmstate: egen per_post_firm_rides=max(per_post_rides)
	bysort firmstate: egen per_pre_firm_NErides=max(per_pre_NErides)
	bysort firmstate: egen per_post_firm_NErides=max(per_post_NErides)
	gen prepostPA_share_rides=per_pre_firm_NErides/per_pre_firm_rides
	gen postpostPA_share_rides=per_post_firm_NErides/per_post_firm_rides
	
	keep priorauth_date prepostPA_share_rides postpostPA_share_rides t state log_NEdial_rides firmstate
}

qui {
	gen months_since=t-659 if (state==34 | state==42 | state==45)
	replace months_since=t-672 if (state==10 | state==11 | state==24 | state==37 | state==51 | state==54)
	replace months_since=. if months_since>23 | months_since<-24
	replace months_since=-50 if missing(months_since) & !(state==34 | state==42 | state==45) & !(state==10 | state==11 | state==24 | state==37 | state==51 | state==54)
	replace months_since=months_since+100
	reghdfe log_NEdial_rides ib99.months_since, a(firmstate t) vce(cluster firmstate)
}
coefplot, keep(*months_since*) vert baselevel ///
	recast(connected) ciopt(recast(rcap)) ///
	xti(Months Since Prior Authorization Implementation) ///
	xlab(1 "-24" 7 "-18" 13 "-12" 19 "-6" 25 "0" ///
		31 "6" 37 "12" 43 "18" 49 "24") ///
	xline(25, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) ///
	yti("Firm-Level Non-Emergency Rides (Logs)") ///
	ylab(,angle(0)) ///
	graphregion(color(white)) bgcolor(white)
qui graph export ${outpath}Figure_A11a.pdf, as(pdf) replace // Figure A11a

qui keep if priorauth_date==1
collapse (mean) prepostPA_share_rides postpostPA_share_rides, by(firmstate)

twoway (hist prepostPA_share_rides if prepostPA_share_rides>0, width(.0249999999) start(0) freq) ///
	(hist postpostPA_share_rides if postpostPA_share_rides>0, fcolor(none) lcolor(black) width(.0249999999) start(0) freq), ///
	yti(Firms) xti("Non-Emergency Share of Rides for Dialysis Patients") ///
	graphregion(color(white)) bgcolor(white) ///
	leg(order(1 "Pre-Prior Auth." 2 "Post-Prior Auth."))
qui graph export "${outpath}Figure_7.pdf", as(pdf) replace // Figure 7

codebook firmstate if prepostPA_share_rides==1 // Referenced in Section 5.3, Paragraph 1
codebook firmstate if postpostPA_share_rides==1 // Referenced in Section 5.3, Paragraph 1

qui {
	gen cat_pre=0 if prepostPA_share_rides==0
	replace cat_pre=1 if prepostPA_share_rides<0.2 & prepostPA_share_rides>0
	replace cat_pre=2 if prepostPA_share_rides<0.4 & prepostPA_share_rides>0.2
	replace cat_pre=3 if prepostPA_share_rides<0.6 & prepostPA_share_rides>0.4
	replace cat_pre=4 if prepostPA_share_rides<0.8 & prepostPA_share_rides>0.6
	replace cat_pre=5 if prepostPA_share_rides<1 & prepostPA_share_rides>0.8
	replace cat_pre=6 if prepostPA_share_rides==1
	gen cat_post=0 if postpostPA_share_rides==0
	replace cat_post=1 if postpostPA_share_rides<0.2 & postpostPA_share_rides>0
	replace cat_post=2 if postpostPA_share_rides<0.4 & postpostPA_share_rides>0.2
	replace cat_post=3 if postpostPA_share_rides<0.6 & postpostPA_share_rides>0.4
	replace cat_post=4 if postpostPA_share_rides<0.8 & postpostPA_share_rides>0.6
	replace cat_post=5 if postpostPA_share_rides<1 & postpostPA_share_rides>0.8
	replace cat_post=6 if postpostPA_share_rides==1
	
	drop if cat_pre==0 & cat_post==0
	drop if cat_pre==. | cat_post==.
	
	bysort cat_pre: gen pre_firms=_N
	bysort cat_pre cat_post: gen pre_post_firms=_N
	gen pre_share=pre_post_firms/pre_firms
}
/*heatplot pre_share cat_pre cat_post, ///
	discrete colors(Blues) val(f(%4.3f)) backfill(i) ///
	cuts(0 .125 .25 .375 .5 .625 .75 .875 1) ///
	keylabels(, inter) leg(subti("Share of" "Pre-Prior Auth." "Firms")) ///
	xti(Post-Prior Auth. Non-Emergency Share) ///
	yti(Pre-Prior Auth. Non-Emergency Share) aspectratio(1) ///
	xlab(0 "Exit" 0.5 "0.0" 1.5 "0.2" 2.5 "0.4" 3.5 "0.6" 4.5 "0.8" 5.5 ///
		"1.0" 6 `" "Only" "Non-" "Emergency" "', labsize(small)) ///
	ylab(0 `" "New" "Entrant" "' 0.5 "0.0" 1.5 "0.2" 2.5 "0.4" 3.5 "0.6" 4.5 "0.8" 5.5 ///
		"1.0" 6 `" "Only" "Non-" "Emergency" "', ///
		labsize(small) angle(0) nogrid) graphregion(color(white))
qui graph export ${outpath}Figure_A20.pdf, replace // Figure A20*/

log close